import launch
import launch_ros
from ament_index_python.packages import get_package_share_directory
import os

import launch_ros.parameter_descriptions

def generate_launch_description():
    # 获取urdf路径
    pkg_path=get_package_share_directory('first_robot')
    urdf_path=os.path.join(pkg_path,'urdf/fishbot','fishbot.urdf.xacro')
    rviz_config=os.path.join(pkg_path,'config','rivz.rviz')

    action_declare_arg_model_path=launch.actions.DeclareLaunchArgument(
        name="model",
        default_value=str(urdf_path),
        description="加载模型文件路径"
    )

    # 通过文件路径，获取内容，并转换参数值对象
    action_cmd=launch.substitutions.Command(
        ['xacro ',launch.substitutions.LaunchConfiguration('model')]
        )
    cmd_value=launch_ros.parameter_descriptions.ParameterValue(
        action_cmd,
        value_type=str
        )

    action_robot_state_pub=launch_ros.actions.Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        parameters=[{'robot_description':cmd_value}]
    )

    action_joint_state_pub=launch_ros.actions.Node(
        package='joint_state_publisher',
        executable='joint_state_publisher',
    )
    action_rviz_node =launch_ros.actions.Node(
        package='rviz2',
        executable='rviz2',
        arguments=['-d' ,rviz_config]
    )

    return launch.LaunchDescription([
        action_declare_arg_model_path,
        action_robot_state_pub,
        action_joint_state_pub,
        action_rviz_node,
    ])